public class Solution565 {
    public int arrayNesting(int[] nums) {
        int[] b=new int[nums.length];
        for (int i=0;i< nums.length;i++){
            b[i]=i;
        }
        for (int i=0;i<nums.length;i++){
            int x=find(b,i);
            int y=find(b,nums[i]);
            b[x]=y;
        }
        int max=0;
        int[] f=new int[nums.length];
        for (int i=0;i<nums.length;i++){
            int x=find(b,i);
            f[x]++;
            max=Math.max(max,f[x]);
        }
        return max;
    }

    int find(int[] b,int t){
        return b[t]==t?t:(b[t]=find(b,b[t]));
    }

    public static void main(String[] args) {
        System.out.println(new Solution565().arrayNesting(new int[]{5,4,0,3,1,6,2}));
    }
}
